package com.mojito.learn.algorithm.leetcode;

/**
 * @author liufq
 * @date 2021-03-03 23:07:03
 */
public class LeetCode11 {

    /**
     * 1137. 第 N 个泰波那契数
     * 动态规划
     */
    public int tribonacci(int n) {
        if (n <= 2) {
            return n == 0 ? 0 : 1;
        }
        int temp, a = 0, b = 1, c = 1;
        for (int i = 3; i <= n; i++) {
            temp = a + b + c;
            a = b;
            b = c;
            c = temp;
        }
        return c;
    }

    /**
     * 1154. 一年中的第几天
     */
    public int dayOfYear(String date) {
        int[] day1 = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] day2 = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        String[] split = date.split("-");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);

        if (month == 1) {
            return day;
        }

        int count = 0;
        for (int i = 0; i < month - 1; i++) {
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                count += day2[i];
            } else {
                count += day1[i];
            }
        }
        return count + day;
    }
}
